/**
 * @project_name staff_vehicle
 * @package_name com.ssh.dao
 * @desc TODO
 * @param
 * @author jack
 * @date 2013-4-25
 * @time 下午3:07:23
 */
package com.ssh.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;

import com.ssh.entity.Scommittee;
import com.ssh.model.pdata.SCommitteeIOPData;

/**
 * @author jack
 * @desc 常委用车DAOI实现
 */
public class ScommitteeDAOImpl implements ScommitteeDAOI {
	private SessionFactory sessionFactory;

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.ScommitteeDAOI#addScommittee(com.ssh.entity.Scommittee)
	 */
	@Override
	public Scommittee addScommittee(Scommittee scommittee) {
		// TODO Auto-generated method stub
		try {
			sessionFactory.getCurrentSession().save(scommittee);
		} catch (Exception e) {
			return null;
		}
		return scommittee;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.ScommitteeDAOI#addScommitteeList(java.util.List)
	 */
	@Override
	public int addScommitteeList(List<Scommittee> scommitteeList) {
		// TODO Auto-generated method stub
		int length = scommitteeList.size();
		try {
			for (int i = 0; i < length; i++)
				addScommittee(scommitteeList.get(i));
		} catch (Exception e) {
			return 0;
		}
		return length;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.ScommitteeDAOI#countById(java.lang.String)
	 */
	@Override
	public int countById(String id) {
		// TODO Auto-generated method stub
		return 0;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.ScommitteeDAOI#deleteById(java.lang.String)
	 */
	@Override
	public int deleteById(String id) {
		// TODO Auto-generated method stub
		int num;
		try {
			String hql = "delete Scommittee as s where s.scommitteeId=?";
			num = sessionFactory.getCurrentSession().createQuery(hql)
					.setString(0, id).executeUpdate();
		} catch (Exception e) {
			return 0;
		}
		return num;
	}

	/**
	 * @return the sessionFactory
	 */
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.ScommitteeDAOI#queryById(java.lang.String)
	 */
	@Override
	public Scommittee queryById(String id) {
		// TODO Auto-generated method stub
		String hql = "from Scommittee as s s.scommitteeId = ?";
		try {
			return (Scommittee) sessionFactory.getCurrentSession()
					.createQuery(hql).setParameter(0, id).uniqueResult();
		} catch (Exception e) {
			return null;
		}

	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.ScommitteeDAOI#queryByIdList(java.lang.String)
	 */
	@Override
	public List<Scommittee> queryByIdList(String strings) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Scommittee queryBySId(String scommitteeId) {
		// TODO Auto-generated method stub
		String hql = "from Scommittee as s where s.scommitteeId =:scommitteeId"
				+ " and s.scommitteeState=:scommitteeState";
		try {
			return (Scommittee) sessionFactory.getCurrentSession()
					.createQuery(hql)
					.setParameter("scommitteeId", scommitteeId)
					.setParameter("scommitteeState", "confirm").uniqueResult();
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public Scommittee queryByVehicleId(String vehicleId,String state) {
		// TODO Auto-generated method stub
		String hql = "from Scommittee as s " +
				" where s.innerVehicle.vehicleId =:vehicleId and s.scommitteeState=:scommitteeState";
		try {
			return (Scommittee) sessionFactory.getCurrentSession()
					.createQuery(hql)
					.setParameter("vehicleId", vehicleId)
					.setParameter("scommitteeState", state).uniqueResult();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * @param sessionFactory
	 *            the sessionFactory to set
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.ssh.dao.ScommitteeDAOI#updateByScommittee(com.ssh.entity.Scommittee)
	 */
	@Override
	public Scommittee updateByScommittee(Scommittee scommittee) {
		// TODO Auto-generated method stub
		try {
			sessionFactory.getCurrentSession().update(scommittee);
			return scommittee;
		} catch (Exception e) {
			return null;
		}

	}


	@Override
	public SCommitteeIOPData querySCCInBySId(String scId) {
		// TODO Auto-generated method stub
		try {
			String hql ="select new com.ssh.model.pdata.SCommitteeIOPData(driver,i,sc,s) " +
					" from Staff s,Staff driver,InnerVehicle i,Scommittee sc" +
					" where sc.scommitteeId=s.staffId and driver.staffId=sc.driverStaffId and sc.scommitteeId=:scommitteeId " +
					" and sc.scommitteeState=:scommitteeState and sc.innerVehicle.vehicleId=i.vehicleId";
			@SuppressWarnings("unchecked")
			List<SCommitteeIOPData> scommitteeIOPData = sessionFactory.getCurrentSession().createQuery(hql).setParameter("scommitteeId", scId).setParameter("scommitteeState", "out").list();
			if (scommitteeIOPData.size() != 0)
				return scommitteeIOPData.get(0);
			else
				return null;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
	}

	@Override
	public SCommitteeIOPData querySCCOutBySId(String scId) {
		// TODO Auto-generated method stub
		try {
			String hql ="select new com.ssh.model.pdata.SCommitteeIOPData(s,i,sc) from Staff s,InnerVehicle i,Scommittee sc" +
					" where sc.scommitteeId=s.staffId and sc.scommitteeId=:scommitteeId " +
					" and sc.scommitteeState=:scommitteeState and sc.innerVehicle.vehicleId=i.vehicleId";
			Query query = sessionFactory.getCurrentSession().createQuery(hql).
					setParameter("scommitteeId", scId).setParameter("scommitteeState", "confirm");
			return (SCommitteeIOPData) query.uniqueResult();
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
	}

	@Override
	public SCommitteeIOPData queryIcanUseByScStateId(String scId) {
		// TODO Auto-generated method stub
		try {
			String hql ="select new com.ssh.model.pdata.SCommitteeIOPData(id,i,sc,s) " +
					"from Staff s,InnerVehicle i,Scommittee sc,InnerDriver id ,Staff sd " +
					" where sc.scommitteeId=s.staffId and sc.scommitteeId=:scommitteeId " +
					" and sc.scommitteeState=:scommitteeState and id.staffId=sd.staffId";

			return (SCommitteeIOPData) sessionFactory.getCurrentSession().createQuery(hql).
				setParameter("scommitteeId", scId).setParameter("scommitteeState", "use").uniqueResult();
			} catch (Exception e) {
				return null;
			}
	}

	@Override
	public SCommitteeIOPData querySCCBySId(String scId,String scstate) {
		// TODO Auto-generated method stub
		try {
			String hql ="select new com.ssh.model.pdata.SCommitteeIOPData(s,i,sc) from Staff s,InnerVehicle i,Scommittee sc" +
					" where sc.scommitteeId=s.staffId and sc.scommitteeId=:scommitteeId " +
					" and sc.scommitteeState=:scommitteeState and sc.innerVehicle.vehicleId=i.vehicleId";

			Query query =  sessionFactory.getCurrentSession().createQuery(hql).
			setParameter("scommitteeId", scId).setParameter("scommitteeState", scstate);
			return (SCommitteeIOPData) query.uniqueResult();
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
	}

}
